package cn.bdqn.action;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.bdqn.bean.Category;
import cn.bdqn.bean.Detail;
import cn.bdqn.dao.CategoryDao;
import cn.bdqn.dao.CategoryDaoImpl;
import cn.bdqn.dao.NewsDao;
import cn.bdqn.dao.NewsDaoImpl;
import cn.bdqn.util.FormUtil;
import cn.bdqn.util.Page;

public class NewsAction {
	public HttpServletRequest request;
	private NewsDao dao = new NewsDaoImpl();

	public String list() {

		Page page = (Page) FormUtil.formToObject(Page.class, request, "utf-8");
		Detail detail = (Detail) FormUtil.formToObject(Detail.class, request,
				"utf-8");
		if (page == null)
			page = new Page();

		dao.findByPage(page, detail);
		request.setAttribute("page", page);
		request.setAttribute("news", detail);
		return "/jsp/admin/newsDetailList.jsp";
	}

	public String toAdd() {
		CategoryDao cd = new CategoryDaoImpl();
		List<Category> categorys = cd.findAll();
		request.setAttribute("categorys", categorys);
		return "/jsp/admin/newsDetailCreate.jsp";
	}

	public String save() {
		Detail detail = (Detail) FormUtil.formToObject(Detail.class, request,
				"utf-8");
		detail.setCreateDate(new Timestamp(System.currentTimeMillis()));
		dao.save(detail);
		return "redirect:/news/list";
	}

	public String del() {
		Detail detail = (Detail) FormUtil.formToObject(Detail.class, request,
				"utf-8");
		int count = dao.del(detail.getId());
		return "redirect:/news/list";
	}
}
